<?php
namespace App\Http\Controllers\Index;
use App\Http\Controllers\Controller;
use BaseClass\Component\Article\ArticleClass;
use BaseClass\Component\Article\ArticleSubject;
use BaseClass\Component\Image\Image;
use BaseClass\Role\User;
use Illuminate\Support\Facades\Request;
use BaseClass\Component\Article\Article;
use BaseClass\Component\Article\ArticleReplyTree;
use Illuminate\Support\Facades\DB;
use GirdPlugins\Base\ArticleFunc;   //张池增加

class BaseController extends Controller {
    public function index()  
    {


        return view("Index.index");



        
        return view("Index.index",$inputData);
    } 
    //从前端输入框获得一个名字为key的输入框，key 就是输入的文章名
   //用此文章名，要打印出关于此文章的所有信息的一个框
  //其中包括查看详情，点击则可以进入详情页面
    public function findArticle()  
    {
        $articleData = Article::select(["search"=>Request::input("key"),"paginate"=>10]);


        $inputData["articleData"] = $articleData["data"];
        $inputData["getSiderbar"] = $this->getSiderbar();

        return view("Index.Article.findArticle",$inputData);
    }
    

    public function sArticle()
    {
        return view("Index.sArticle");
    }
    
    public function userIndex($user_id)  //用户首页
    {
        //获取用户信息

        $userObj = new User($user_id);
        $inputData['userData'] = $userObj->info;

                    
        //获得当前用户的所有文章
        $inputData['articleData'] = Article::select(["user"=>$user_id, "desc"=>true,"sort"=>"article_id","paginate"=>10,"num"=>10])["data"];

        //获取当前用户的专题
        $inputData["subjectData"] = ArticleSubject::getMoreByUser($user_id);
        //获取图片
        $inputData["imageData"] = Image::select(["user"=>$user_id, "desc"=>true,"sort"=>"image_id","num"=>9,"paginate"=>6])["data"];

        return view("Index.User.userIndex",$inputData);
        
        
    }
    
    
   public function sSubject($class_id = 0)    //得到所有专题，并列出来
    {

       $inputData['subject']  = ArticleSubject::select([ "desc"=>true,"sort"=>"subject_id","paginate"=>10])["data"];
       $inputData["class_name"] = "全部专题";
        return view("Index.Subject.sSubject",$inputData);
    }
    
   public function moreSubject($subject_id)     //传递满足$subject_id的文章
    {
        $subjectObj  = new ArticleSubject($subject_id);
        $subjectObj -> syncArticleInfo();
       $inputData['articleData'] = $subjectObj->article_info;

       $inputData['subjectData'] =  $subjectObj ->subject_info;

        $inputData["userInfoGui"] = $this->userSider($inputData["subjectData"]->subject_user);


        return view("Index.Subject.moreSubject",$inputData);
        
    }
    
    public function articleDetail($article_id)
    {
        $articleObj = new Article($article_id);
        $viewData["articleData"] =  $articleObj->info;
        $collectStatus = DB::table("base_article_collect")
                ->where("collect_user","=",session("user.user_id"))
                ->where("collect_article_id","=",$article_id)
                ->first();
        $collectStatus!=NULL?$viewData["collectStatus"]=true:$viewData["collectStatus"]=false;
        

        $viewData["choseData"] =NULL;
        $viewData["classData"]=DB::table('base_article_collect_class')->get();
          $limit_tree["article_id"] = $article_id;
          $limit_tree["join_user"] = true;
          $limit_tree["join_relation"] = true;
          $tree = new ArticleReplyTree($limit_tree);
          $viewData["replyData"]  = $tree -> buildArticleReplyTree();


        //zc

        $viewData["userInfoGui"] = $this->userSider($viewData["articleData"]->article_user);
        $viewData["sidebarRecommendGui"] = $this->getSiderbar();
        return view("Index.Article.articleDetail",$viewData);
       
    }

    /*按类别查看文章*/
    public function sDisplayArticleClass($class_id = 0)
    {
                
        $viewData["classData"] = ArticleClass::select([])["data"];
        if($class_id == 0)
        {
            $viewData["articleData"] = Article::select(["desc"=>true,"sort"=>"article_id","paginate"=>10])["data"];
           $viewData["nowClassName"] = "所有文章";
           $viewData["nowClassId"] = NULL;
            
        }
        else
        {
            $viewData["articleData"] = Article::select(["desc"=>true,"sort"=>"article_id","class"=>$class_id,"paginate"=>10])["data"];
                foreach($viewData["classData"] as $data)
                {
                    if($data->class_id == $class_id)
                    {
                        $viewData["nowClassName"] = $data->class_name;
                        $viewData["nowClassId"] = $data->class_id;
                        break;
                    }
                }
        }

        $viewData["articleClassBar"] =$this->sidebarClass();
        $viewData["indexRecommendArticle"] = $this->getSiderbar();
        return view("Index.sDisplayArticleClass", $viewData);
    }
    
    
    /*各种组件模块*/
    
    
    
    
    //用户侧栏组件
    private function userSider($user_id)
    {
        $viewData["userData"] =  DB::table('base_user')
                ->leftJoin("base_user_relation","relation_focus","=","user_id")
                ->where("user_id","=",$user_id)
                ->leftJoin("base_image","image_id","=","user_image")
                ->first();
         return view("Index.User.userSider",$viewData);
    }
    private function getSiderbar()
    {
        $inputData["articleData"] = Article::select(["desc"=>true,"sort"=>"article_click","num" => 8])["data"];
        return view("Index.Gui.getSiderbar",$inputData);
    }

    //侧栏类别组件  需要一个单独的按类查找的页面
    private function sidebarClass($url = "index_sDisplayArticleClass")
    {
        $viewData["classData"] = ArticleClass::select(["desc"=>true])["data"];
        $viewData["url"] = $url;
        return view("Index.Gui.sidebarClass",$viewData);
    }
    


    /*
    //显示一个文章类的概览
    private function displayArticleClassBar($class_id,$num = 5)
    {
        $viewData["classData"] = DB::table("base_display_class")->where("class_id","=",$class_id)->first();
        $viewData["articleData"] = DB::table("base_display_article_recommend")
                ->where("recommend_class","=",$class_id)
                ->leftJoin("base_article","article_id","=","recommend_article")
                ->join("base_user","user_id","=","article_user")
                ->skip(0)->take($num)
                //->orderBy("recommend_sort","desc")
                ->get();
        return view("Index.Gui.displayArticleClassBar",$viewData);
    }
    //一个推荐专题类的板块
    private function displaySubjectClassBar($class_id,$num = 5)
    {
        $viewData["classData"] = DB::table("base_display_class")->where("class_id","=",$class_id)->first();
        $viewData["subjectData"] = DB::table("base_display_subject_recommend")
                ->where("recommend_class","=",$class_id)
                 ->join("base_user","user_id","=","article_user")
                ->leftJoin("base_subject","subject_id","=","recommend_subject")
                ->skip(0)->take($num)
                //->orderBy("recommend_sort","desc")
                ->get();
        return view("Index.Gui.displaySubjectClassBar",$viewData);
    }
    
    //最新文章板块
    private function newArticle($num = 5)
    {
        $viewData["articleData"] = DB::table("base_article")
                ->orderBy("article_id","desc")
                ->skip(0)->take($num)
                 ->join("base_user","user_id","=","article_user")
                ->get();
        return view("Index.Gui.newArticle",$viewData);
    }
    
    //首页推荐栏
    private function indexRecommendArticle($num = 5)
    {
        $viewData["indexData"] = DB::table("base_index_display")
                ->where("display_location","=",0)
                ->leftJoin("base_article","article_id","=","display_article_id")
                  ->join("base_user","user_id","=","article_user")
                ->orderBy("display_sort","desc")->skip(0)->take($num)->get();
        return view("Index.Gui.indexRecommendArticle",$viewData);
    }
    //侧栏类别组件  需要一个单独的按类查找的页面
    private function sidebarClass($url = "index_sDisplayArticleClass")
    {
        $viewData["classData"] = DB::table("base_display_class")
               ->orderBy("class_sort","desc")->get();
        $viewData["url"] = $url;
        return view("Index.Gui.sidebarClass",$viewData);
    }
    //侧栏推荐文章
    private function sidebarRecommendArticle($num = 5)
    {
         $viewData["displayData"] = DB::table("base_index_display")
                 ->where("display_location","=",2)
               ->leftJoin("base_article","article_id","=","display_article_id")
                   ->join("base_user","user_id","=","article_user")
                ->orderBy("display_sort","desc")->skip(0)->take($num)->get();
        return view("Index.Gui.sidebarRecommendArticle",$viewData);
    }*/



}
